package emergency;

import java.util.ArrayList;

import unit.Ambulance;
import unit.FireTruck;
import unit.PoliceCar;
import unit.Unit;

public class RequirementDefaultEmergency extends RequirementsEmergency {

	/**
	 * Checks if this requirement is the right one to handle the given emergency
	 * 
	 * @param emergency
	 *            the emergency to check if this Requirement needs to handle it
	 * 
	 * @return Returns true if this is the right requirement for the given
	 *         emergency, false if not
	 */
	@Override
	boolean canHandleEmergency(Emergency emergency) {
		return true;
	}

	/**
	 * Method that will create the suggestion for the given emergency, with the
	 * given units
	 * 
	 * @param emergency
	 *            the emergency we need to compute the suggestion for
	 * 
	 * @param units
	 *            list of units of which the needed units will be picked
	 * 
	 * @post the total amount of ambulances needed is set
	 * 
	 * @post the total amount of firetrucks needed is set
	 * 
	 * @post the total amount of policecars needed is set
	 * 
	 * @return Returns a list of suggested units to assign to the emergency
	 */
	@Override
	ArrayList<Unit> createSuggestion(Emergency emergency, ArrayList<Unit> units) {
		ArrayList<Unit> availableUnits = new ArrayList<Unit>();
		setRequirements(emergency);

		for (Unit u : units) {
			if (u instanceof Ambulance) {
				if (getTotalAmbulancesNeeded() > 0) {
					availableUnits.add(u);
					setTotalAmbulancesNeeded(getTotalAmbulancesNeeded() - 1);
				}
			} else if (u instanceof FireTruck) {
				if (getTotalFireTrucksNeeded() > 0) {
					availableUnits.add(u);
					setTotalFireTrucksNeeded(getTotalFireTrucksNeeded() - 1);
				}
			} else if (u instanceof PoliceCar) {
				if (getTotalPoliceCarsNeeded() > 0) {
					availableUnits.add(u);
					setTotalPoliceCarsNeeded(getTotalPoliceCarsNeeded() - 1);
				}
			}
		}
		return availableUnits;
	}

	/**
	 * Method for setting the total units needed, depending on this requirement
	 * 
	 * @post The total amount of ambulances needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of policecars needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of firetrucks needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of units needed is set, depending on this
	 *       requirement
	 */
	@Override
	void setRequirements(Emergency emergency) {
		setTotalAmbulancesNeeded(1);
		setTotalFireTrucksNeeded(1);
		setTotalPoliceCarsNeeded(1);

		setTotalUnitsNeeded(getTotalAmbulancesNeeded()
				+ getTotalFireTrucksNeeded() + getTotalPoliceCarsNeeded());

	}
}
